<?php
/**
 * @software         [JiJinCMS]
 * @author           [上海集锦信息科技有限公司]
 * @website          [http://www.jijinweb.com]
 * @email            [sunny@jijinweb.com]
 **/
namespace app\admin\controller;
use think\facade\View;
use app\admin\model\RoleModel;
use think\facade\Db;
use think\facade\Request;
class Link extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->model=new \app\admin\model\LinkModel();
        $this->validate=new \app\admin\validate\LinkValidate();
        View::assign('cat', Db::name("link_cat")->order("sort asc,catid asc")->select());
        View::assign('pubid',"202105261114");//pubid为固定的
        if(Request::param("catid")){
            View::assign("catid",Request::param("catid"));        
        }           
    }     
    //
    public function index(){		
        $where=[];
        if(Request::param("catid")){
            $where["catid"]=Request::param("catid",0,"intval");            
        }
        //查询与分页
        $listRows=Request::param("listRow")?Request::param("listRow"):20;//每页显示多少条
        $data=$this->model->where($where)->order("id desc")->paginate(['list_rows'=>$listRows,'query' => request()->param()]);
        foreach($data as $key=>$v){
            $data[$key]['cat']=Db::name("link_cat")->find($v['catid']);
        }
        // 获取分页显示
        $pagelist = $data->render();        
        View::assign("pagelist",$pagelist);
        View::assign('data', $data);
        return View::fetch();
    }
    //
    public function add(){
        if(request()->isPost()){
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            $this->model->save($data);
            $id=$this->model->id;
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$id,$this->request->param());            
            $this->success($id);
        }else{                     
            return View::fetch();
        }
    }
    public function update(){
        if(request()->isPost()){
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            $data["status"]=$data["status"]?1:0;
            $this->model->update($data);
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$data['id'],$this->request->param());            
            $this->success($catid);
        }else{
            $id=Request::param('id',0,"intval");
            $r=$this->model->find($id);
            View::assign("r",$r);        
            return View::fetch();
        }
    } 
    //
    public function delete(){
        $ids=Request::param("ids",0,"intval");
        if(!$ids){
             $this->error=('请选择信息！');
        }
        foreach($ids as $id){
            $this->model->destroy($id);
        } 
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$ids,$this->request->param());  
        $this->success("删除成功");
    }
    //设置状态是否启用
    public function setStatus(){
        $id=Request::param('id',0,"intval");
        $status=Request::param('status',0,"intval");
        $this->model->update(['id'=>$id,'status'=>$status]);
        //记录日志
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$id);        
        $this->success("设置成功");
    }     
    //字段排序
    public function updateSort(){
        $ids=Request::param("ids");
        $sort=Request::param("sort");
        foreach($ids as $key=>$id){
            $data['id']=(int)$id;
            $data['sort']=(int)$sort[$key];
            $this->model->update($data);
        }
        $this->success("排序更改成功");
    }    
        public function export(){
        $where=[];
        $catid=Request::param("catid");
        if($catid){
            $where["catid"]=$catid;
        }
        $data=$this->model->where($where)->order("id desc")->select();

        $objPHPExcel = new \PHPExcel();
        //接下来就是写数据到表格里面去  
        $objActSheet = $objPHPExcel->getActiveSheet(0);  
        //表头
        $objActSheet->setCellValue('A1',"名称");
        $objActSheet->setCellValue('B1',"链接地址");
        //内容
        foreach($data as $k1=>$v){
                $objActSheet->setCellValue(IntToChr(0).($k1+2),$v['name']);
                $objActSheet->getStyle(IntToChr(0).($k1+2))->getAlignment()->setWrapText(true);
                $objActSheet->setCellValue(IntToChr(1).($k1+2),$v['link']);
                $objActSheet->getStyle(IntToChr(1).($k1+2))->getAlignment()->setWrapText(true);                
        }
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="信息反馈-'.$cat['name'].'.xls"');
        header("Content-Disposition:attachment;filename=信息反馈-".$cat['name'].".xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');    
    }  
}
